from common.config import *
from common.log_info import Logger

headers_merchant = {"Content-Type": "application/json;charset=UTF-8", "application": "cms"}


class Organization:
    """组织模块"""

    def __init__(self):
        self.logger = Logger("organization", "organization").logger

    def login(self, env, data):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/session/login"
        self.logger.info(f"登录的请求参数：{data}")
        return requests.post(url, json=data, headers=headers_merchant)

    # 增加组织
    def add_branch(self, env, parent_id, username, branch_fullname, branch_simple_name, role_id, headers):
        """parentId：父节点ID
        username：管理员账号
        branch_fullname：组织节点名称（全称）
        branch_simple_name：组织节点名称（简称）
        role_id：关联角色ID"""
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/branch"
        data = {
            "parentId": parent_id,
            "userName": username,
            "userEmail": "",
            "branchFullName": branch_fullname,
            "branchSimpleName": branch_simple_name,
            "branchAddress": "颐东苑北二院西1号楼",
            "branchType": "MANAGEMENT_CENTER",
            "lng": "111.6716187745408",
            "lat": "40.86387763498812",
            "countryId": 100000,
            "roles": [{
                "id": role_id
            }],
            "regionId": 150102
        }
        self.logger.info(f"增加组织的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 编辑组织中心
    def edit_branch(self, env, data, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/branch"
        self.logger.info(f"编辑组织中心的请求参数：{data}")
        return requests.put(url, headers=headers, json=data)

    # 删除组织中心
    def delete_branch(self, env, branch_id, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"buorg/v1/branch/{branch_id}"
        self.logger.info(f"删除组织节点的id：{branch_id}")
        return requests.delete(url, headers=headers)

    # 中心详情
    def get_branch_info(self, env, branch_id, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"buorg/v1/branch/{branch_id}"
        self.logger.info(f"查询节点branch_id：{branch_id}")
        return requests.get(url, headers=headers)

    # 组织账号列表
    def account_list(self, env, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/user/pageList"
        data = {
            "branchSimpleName": "",
            "account": ""
        }
        self.logger.info(f"查询组织账号列表请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 新增组织账号
    def create_account(self, env, account, branch_id, role_id, headers):
        """"account:管理员账号
            branchId：当前节点ID
            role_id：关联角色ID"""
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/user/createUser"
        data = {"account": account, "email": "", "branchId": branch_id, "securityRoles": [{"id": role_id}]}
        self.logger.info(f"新增组织账号的请求参数：{data}")
        return requests.post(url, json=data, headers=headers)

    # 查看组织账号详情
    def account_info(self, env, account_id, headers):
        """account_id:账号id"""
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/user/userInfo"
        data = {"id": account_id}
        self.logger.info(f"查询组织账号详情请求参数：{data}")
        return requests.post(url, json=data, headers=headers)

    # 修改组织账号
    def update_account(self, env, account_id, email, role_id, headers):
        """account_id：当前账号id，
            email：邮箱地址
            role_id：关联角色id"""
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/user/updateUser"
        data = {"id": account_id, "email": email, "securityRoles": [{"id": role_id}]}
        self.logger.info(f"修改组织账号的请求参数：{data}")
        return requests.post(url, json=data, headers=headers)

    # 修改账号状态
    def edit_account_status(self, env, account_id, status, headers):
        """account_id：当前账号id，"""
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/user/updateUserStatus"
        data = {"id": account_id, "status": status}
        self.logger.info(f"修改账号状态的请求参数：{data}")
        return requests.post(url, json=data, headers=headers)

    # 修改密码
    def update_account_pwd(self, env, user_id, pwd, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/user/updatePwdByBId"
        data = {"newPwd": pwd, "isMail": 0, "userId": user_id}
        self.logger.info(f"修改密码的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 角色权限添加和移除
    def permission_regulate(self, env, data, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/security/role/permission/save"
        self.logger.info(f"组织角色添加权限的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 添加角色
    def add_role(self, env, data, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/security/role/mcreate"
        self.logger.info(f"添加组织角色的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 编辑角色
    def update_role(self, env, data, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/security/role/update"
        self.logger.info(f"编辑角色的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 角色详情
    def role_detail(self, env, role_id, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/security/role/detail"
        self.logger.info(f"查询角色详情的请求参数：{role_id}")
        return requests.get(url, headers=headers, params={"id": role_id})

    # 删除角色
    def delete_role(self, env, role_id, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"buorg/v1/security/role/{role_id}"
        self.logger.info(f"删除角色的请求参数：{role_id}")
        return requests.delete(url, headers=headers)

    # 角色添加组织节点
    def role_add_nodes(self, env, role_id_list, branch_id_list, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/branch/roles"
        self.logger.info(f"角色添加组织节点的请求参数：{role_id_list},{branch_id_list}")
        return requests.patch(url, headers=headers, json=role_id_list, params={"ids": branch_id_list})

    # 角色下移除组织节点
    def role_remove_nodes(self, env, role_id_list, branch_id_list, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/branch/roles"
        self.logger.info(f"角色移除组织节点的请求参数：{role_id_list},{branch_id_list}")
        return requests.delete(url, headers=headers, json=role_id_list, params={"ids": branch_id_list})

    # 新增员工
    def add_employee(self, env, data, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/employee/create"
        self.logger.info(f"新增员工的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 修改员工信息
    def update_employee(self, env, data, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/employee/update"
        self.logger.info(f"修改员工信息请求参数:{data}")
        return requests.post(url, headers=headers, json=data)

    # 修改员工状态
    def employee_status(self, env, data, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/employee/changeStatus"
        self.logger.info(f"冻结员工的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 修改员工密码
    def update_password(self, env, data, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/employee/updatePassword"
        self.logger.info(f"修改员工密码的请求参数：{data}")
        return requests.post(url, headers=headers, json=data)

    # 员工信息
    def employee_details(self, env, emp_id, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"buorg/v1/acl/employee/{emp_id}"
        self.logger.info(f"查询员工信息请求参数：{emp_id}")
        return requests.get(url, headers=headers)

    # 删除员工
    def delete_employee(self, env, emp_id, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"buorg/v1/acl/employee/{emp_id}"
        self.logger.info(f"删除员工信息请求参数：{emp_id}")
        return requests.delete(url, headers=headers)

    # 列表员工
    def employee_list(self, env, branch_id, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/employee/"
        self.logger.info(f"员工列表归属的branchid：{branch_id}")
        return requests.get(url, headers=headers, params={"branchId": branch_id, "current": 1, "size": 5})

    # 员工角色关联员工
    def emp_role_add_emp(self, env, role_id_list, emp_id_list, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/branch/employeeRoles"
        self.logger.info(f"员工角色关联员工的角色id：{role_id_list},员工id：{emp_id_list}")
        return requests.patch(url, headers=headers, json=role_id_list, params={"ids": emp_id_list})

    # 员工角色移除员工
    def emp_role_remove_emp(self, env, role_id_list, emp_id_list, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/branch/employeeRoles"
        self.logger.info(f"员工角色移除员工的角色id：{role_id_list},员工id：{emp_id_list}")
        return requests.delete(url, headers=headers, json=role_id_list, params={"ids": emp_id_list})

    # 角色下的员工列表
    def role_emp_list(self, env, branch_id, role_id, headers):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "buorg/v1/acl/employee/getEmployeeListByRoleId"
        data = {"branchId": branch_id, "roleId": role_id, "current": 1, "size": 5}
        self.logger.info(f"查询角色下的员工列表的请求参数：{data}")
        return requests.get(url, headers=headers, params=data)
